<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择商品状态" @change="handleQuery">
          <el-option label="全部" value=""/>
          <el-option label="已上线" value="online"/>
          <el-option label="已下线" value="offline"/>
          <el-option label="待上线" value="prepare"/>
        </el-select>
      </el-form-item>

      <el-form-item label="删除状态" prop="delFlag">
        <el-select v-model="queryParams.delFlag" placeholder="请选择删除状态" @change="handleQuery">
          <el-option label="全部" value=""/>
          <el-option label="正常" :value="0"/>
          <el-option label="已删除" :value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:goods:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList">
      <el-table-column label="商品ID" align="center" prop="id"/>
      <el-table-column label="商品名称" align="center" prop="name"/>
      <!--      <el-table-column label="logo" align="center" prop="logo"/>-->
      <el-table-column label="划线价" align="center" prop="crossedPrice">
        <template slot-scope="scope">
          <span>¥{{ scope.row.crossedPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平台价" align="center" prop="platformPrice">
        <template slot-scope="scope">
          <span>¥{{ scope.row.platformPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'online'" type="success">已上线</el-tag>
          <el-tag v-else-if="scope.row.status === 'offline'" type="danger">已下线</el-tag>
          <el-tag v-else-if="scope.row.status === 'prepare'" type="warning">待上线</el-tag>
          <span v-else>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除状态" align="center" prop="delFlag">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.delFlag == '0'" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.delFlag == '1'" type="danger">已删除</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="销量" align="center" prop="saleQuantity"/>
      <el-table-column label="标签" align="center" prop="label">
        <template slot-scope="scope">
          <el-tag
            v-for="(tag, index) in scope.row.label ? scope.row.label.split(',') : []"
            :key="index"
            style="margin-right: 5px;"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="stock"/>
      <!--      <el-table-column label="创建者" align="center" prop="createrBy"/>-->
      <!--      <el-table-column label="更新者" align="center" prop="updateBy"/>-->
      <el-table-column label="排序权重" align="center" prop="weight"/>
      <el-table-column label="添加时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.delFlag == 0 && scope.row.status === 'online'"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleChangeStatus(scope.row, 'offline')"
            v-hasPermi="['cms:goods:edit']"
          >下线
          </el-button>
          <el-button
            v-if="scope.row.delFlag == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:goods:edit']"
          >修改
          </el-button>
          <el-button
            v-if="scope.row.delFlag == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:goods:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {delGoods, getGoods, listGoods, updateGoods} from "@/api/cms/goods"

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      goodsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        status: null,
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询商品管理列表 */
    getList() {
      this.loading = true
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: "/goods/add"});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.$router.push({path: "/goods/edit/" + id});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 下线操作 */
    handleChangeStatus(row, status) {
      const tip = status === 'offline' ? '下线' : '上线';
      this.$modal.confirm(`是否确认${tip}商品编号为"${row.id}"的数据项？`).then(() => {
        // 获取商品详情
        return getGoods(row.id);
      }).then(response => {
        // 更新商品状态
        const goodsData = response.data;
        goodsData.status = status;
        return updateGoods(goodsData);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(`${tip}成功`);
      }).catch(() => {
      });
    }
  }
}
</script>
